from pyecharts.charts import Line
import pyecharts.options as opts
from pyecharts.faker import Faker
import numpy as np

price = []
yprice = []
def test():
    global price
    global yprice
    import pandas as pd
    from sqlalchemy import create_engine
    engine = create_engine('mysql+pymysql://root:123456@localhost:3306/pythondata')
    # 查询语句，选出employee表中的所有数据
    sql = ''' select price from test;'''
    # read_sql_query的两个参数: sql语句， 数据库连接
    city = pd.read_sql_query(sql, engine)
    price = np.array(city)  # 先将数据框转换为数组
    price = (price.tolist())[:6]  # 其次转换为列表

    sql = ''' select yprice from test;'''
    # read_sql_query的两个参数: sql语句， 数据库连接
    city = pd.read_sql_query(sql, engine)
    yprice = np.array(city)  # 先将数据框转换为数组
    yprice = (yprice.tolist())[:6]  # 其次转换为列表


line1=(
    Line() # 生成line类型图表
    .add_xaxis(Faker.choose())  # 添加x轴，Faker.choose()是使用faker的随机数据生成x轴标签
    .add_yaxis('数据1',yprice)  # 添加y轴，Faker.values()是使用faker的随机数据生成y轴数值
    # .add_yaxis('数据2',Faker.values())
    .set_global_opts(title_opts=opts.TitleOpts(title='Line 基本示例'))
)
test()
print(price)
print(yprice)

# print(type(data))
# print(Faker.choose())
# print(Faker.values())
# print(Faker.values())
# print(type(Faker.values()))
# print(type())
line1.render('temp2.html') # 生成一个名为pyecharts-line.html的网页文件，打开网页就是下图
